package com.hmall.order.handler;

/**
 * 规定所有的Http头中必须携带Token 来验证用户身份，采用在拦截器用户名信息绑定到ThreadLocal，以供后续方法使用
 * 声明 上下文
 */
public class RequestHeaderContext {

    private static final ThreadLocal<RequestHeaderContext> REQUEST_HEADER_CONTEXT_THREAD_LOCAL=new ThreadLocal<>();
    private String userName;
   // private String token;

    public String getUserName() {
        return userName;
    }

    /*public String getToken() {
        return token;
    }*/

    public static RequestHeaderContext getInstance(){
        return REQUEST_HEADER_CONTEXT_THREAD_LOCAL.get();
    }

    public void setContext(RequestHeaderContext context){
        REQUEST_HEADER_CONTEXT_THREAD_LOCAL.set(context);
    }

    public static void clean(){
        REQUEST_HEADER_CONTEXT_THREAD_LOCAL.remove();
    }

    private RequestHeaderContext(RequestHeaderContextBuild requestHeaderContextBuild){
        this.userName=requestHeaderContextBuild.userName;
        // this.token=requestHeaderContextBuild.token;
        setContext(this);
    }

    public static class RequestHeaderContextBuild{
        private String userName;
        // private String token;

        public RequestHeaderContextBuild userName(String userName){
            this.userName=userName;
            return this;
        }

        /*public RequestHeaderContextBuild token(String token){
            this.token=token;
            return this;
        }*/
        public RequestHeaderContext bulid(){
            return new RequestHeaderContext(this);
        }
    }
}

